package impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import baise.BaseDaoImpl;
import baise.iHibBaseDAO;
import baise.iHibBaseDAOimpl;
import model.TBook;
import DAO.TBookDAO;

public class TBookDAOimpl implements TBookDAO {
	iHibBaseDAO iBaseDAO=new iHibBaseDAOimpl();
	@Override
	public int addbook(TBook book) {
		if (book.getLocationid()!=null) {
			 
			return (Integer) iBaseDAO.insert(book);
		}
		return 0;
		
	}

	@Override
	public boolean deletebook(TBook book) {
		
		return iBaseDAO.delete(book);
	}

	@Override
	public boolean updatebook(TBook book) {
		
		return iBaseDAO.update(book);
	}

	@Override
	public TBook selectBook(TBook book){
		TBook tb = (TBook)iBaseDAO.findById(TBook.class,book.getId());
		return tb;
	}
	@Override
	public List<TBook> selecTBooks() throws SQLException {
		String sql="select top 5 name,score,locationid,photoid from T_Book where  locationid is not null order by score desc";
 		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet list=bea.select(sql);
		List<TBook> list2=convertList(list);
		return list2;
	}
	@Override
	public List<TBook> selectBook(String link) {
		String sql="select * from T_Book where  locationid is not null and keyword like '%"+link+"%'";
		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet rs=bea.select(sql);
		List<TBook> list;
		try {
			list = convertList(rs);
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

 	@Override
	public List<TBook> selectallbynull() throws SQLException {
 		String sql="select top 6 * from T_Book where locationid is not null";
 		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet list=bea.select(sql);
		List<TBook> list2=convertList(list);
		return list2;
	}
 	@Override
	public List<TBook> selectall() throws SQLException{
 		String sql="select * from T_Book";
 		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet list=bea.select(sql);
		List<TBook> list2=convertList(list);
		return list2;
	}
 	
	public static void main(String[] args) throws SQLException {
		TBookDAO tb=new TBookDAOimpl();
	List<TBook> list=tb.selectBook("1");
	System.out.println(list);
//		TBook tbk=new TBook();
//		tbk.setId(1);
//		tb.selectBook(tbk);
		String jsonString=JSON.toJSONString(list);
		System.out.println(jsonString);
//		tbk.setName("32");
//		tbk.setLocationid(108);
		//ResultSet list=
		//List<TBook> list2=tb.selecTBooks();
		//String json=JSON.toJSONString(list2);
		//System.out.println(list2);
	}
	
	
	/**
	 * ������
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	
	private static List convertList(ResultSet rs) throws SQLException{
		List list = new ArrayList();
		ResultSetMetaData md = rs.getMetaData();//��ȡ����
		int columnCount = md.getColumnCount();//��ȡ�е�����
		while (rs.next()) {
			Map rowData = new HashMap();//����Map
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));//��ȡ����ֵ
				}
				list.add(rowData);
			}
		return list;
	}

	

	
}
